package org.example.demo.pojo;

import org.example.demo.beans.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DepDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int add(Department department){
        return jdbcTemplate.update("insert into department (dname) values (?)",department.getDname());
    }

    public int del(Integer id){
        return jdbcTemplate.update("delete from department where id=?",id);
    }

    public int update(Department department){
        return jdbcTemplate.update("update department set dname=? where id=?",department.getDname(),department.getId());
    }

    public Department get(Integer id){
        return jdbcTemplate.queryForObject("select * from department where id=?", BeanPropertyRowMapper.newInstance(Department.class),id);
    }

    public List<Department> getAll(){
        return jdbcTemplate.query("select * from department",BeanPropertyRowMapper.newInstance(Department.class));
    }
}
